package com.wahoyu.exportwordtest.domain;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Objects;

/**
 * @ClassName ChunkMultipartFile
 * @Description
 * @Author Quyq
 * @Date 2023/3/15 15:20
 **/
public class ChunkMultipartFile implements MultipartFile {

    public ChunkMultipartFile(String originalFilename , String contentType,byte[] datas){
        this.name = originalFilename;
        this.originalFilename = originalFilename;
        this.contentType = contentType;
        this.datas = datas;
    }

    private final String name;

    private final String originalFilename;

    private final String contentType;

    private final byte[] datas;

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getOriginalFilename() {
        return originalFilename;
    }

    @Override
    public String getContentType() {
        return contentType;
    }

    @Override
    public boolean isEmpty() {
        return Objects.isNull(datas) || datas.length == 0;
    }

    @Override
    public long getSize() {
        return Objects.isNull(datas) ? 0 : datas.length;
    }

    @Override
    public byte[] getBytes() throws IOException {
        return datas;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(datas);
    }

    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
        try (FileOutputStream f = new FileOutputStream(dest)) {
            f.write(datas);
        }
    }

}